package Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Review {
    public static void main(String[] args) {
        shangfen s = new shangfen();
        person newinstans =(person) proxyFactory1.getNewinstans(s);
        newinstans.daidawangzhe();
        System.out.println("**********************");
        sellsome s1 = new sellsome();
        person newinstans1 = (person)proxyFactory1.getNewinstans(s1);
        newinstans1.daidawangzhe();
        System.out.println("**********************");
        wanyouxi s2 = new wanyouxi();
        person newinstans2 = (person)proxyFactory1.getNewinstans(s2);
        newinstans2.daidawangzhe();
    }
}
interface person{
       void daidawangzhe();
}
//被代理类
class shangfen implements person{

    @Override
    public void daidawangzhe() {
        System.out.println("帮我打王者 打到王者50颗星");
    }
}
class sellsome implements person {

    @Override
    public void daidawangzhe() {
        System.out.println("帮我卖50斤大白菜");
    }
}
class wanyouxi implements person{

    @Override
    public void daidawangzhe() {
        System.out.println("帮我玩QQ三国");
    }
}
//动态代理
class proxyFactory1{
    public static Object getNewinstans(Object obj){
        Myinvocation s = new Myinvocation();
        s.bind(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                s);
    }
}
class Myinvocation implements InvocationHandler {
   private Object obj;
   public void bind(Object obj){
       this.obj=obj;
   }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(obj,args);
    }
}


